/*
  {
    "local_region": {
      url: '',
    },
    "sales_country_ob": {
      url: '',
    },
    "sales_region_ob": {
      url: '',
    },
    "trade_line": {
      url: '',
    },
    "sales_country_ib": {
      url: '',
    },
    "sales_region_ib": {
      url: '',
    }
  },
*/
export class Results {
  private _year: number;
  private _week: number;
  private _timestamp: string;
  private _files: Array;
  private _file_names: Array;


  public get year(): number{
    return this._year;
  }
  public get week(): number{
    return this._week;
  }
  public get timestamp(): string{
    if(this._timestamp.length > 0)
      return this._timestamp;
    return "";
  }
  public get files(): Array {
    return this._files;
  }
  public get file_names(): Array{
    return this._file_names;
  }


  constructor(year:number=2024, week:number=1) {
    this._year = year;
    this._week = week;
    this._file_names = [
      "local_region",
      "trade_line",
      "sales_country_ob",
      "sales_country_ib",
      "sales_region_ob",
      "sales_region_ib",
    ];
    this._files = [];
    this._timestamp = ''
  }

  public set_all(year:number, week:number, file_urls: Map): boolean {
    this.clear();
    const r = this._set_year_week(year, week) && this._set_files(file_urls)
    this._refresh();
    return r;
  }

  private _refresh(): void{
    const d = new Date();
    this._timestamp = d.toLocaleDateString('chinese') + " " + d.toLocaleTimeString('chinese', {hour12:false});
  }


  public _set_year_week(year:number, week:number): boolean {
    this._year = year;
    this._week = week
    return true
  }
  public _set_files(file_urls: Map) : boolean {
    return this._file_names.every(
      (fn) => {
        const v =  file_urls[fn];
        if(v!== undefined && v !== null
          && v["url"] !== undefined && v["url"] !== null) 
        {
          this._files.push({
              url: v.url,
              label: v.label,
              fname: v.fname,
          });
          return true;
        }
        else {
          return false;
        }
      }
    )
  }
  public clear(){
    this._files = [];
  }
}
